<?php

namespace Database\Seeders;

use App\Models\Permission;
use App\Models\Role;
use Exception;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class PermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 重置角色和权限缓存
        app()['cache']->forget('spatie.permission.cache');

        $permissions = [
            ['name' => 'permission', 'cn_name' => '权限管理', 'pid' => 0, 'guard_name' => 'api'],
            ['name' => 'permission.index', 'cn_name' => '权限列表', 'pid' => 1, 'guard_name' => 'api'],
            ['name' => 'permission.store', 'cn_name' => '添加权限', 'pid' => 1, 'guard_name' => 'api'],
            ['name' => 'permission.update', 'cn_name' => '修改权限', 'pid' => 1, 'guard_name' => 'api'],
            ['name' => 'permission.destroy', 'cn_name' => '删除权限', 'pid' => 1, 'guard_name' => 'api'],

            ['name' => 'roles', 'cn_name' => '角色管理', 'pid' => 0, 'guard_name' => 'api'],
            ['name' => 'roles.index', 'cn_name' => '角色列表', 'pid' => 6, 'guard_name' => 'api'],
            ['name' => 'roles.store', 'cn_name' => '添加角色', 'pid' => 6, 'guard_name' => 'api'],
            ['name' => 'roles.show', 'cn_name' => '角色详情', 'pid' => 6, 'guard_name' => 'api'],
            ['name' => 'roles.update', 'cn_name' => '角色修改', 'pid' => 6, 'guard_name' => 'api'],
            ['name' => 'roles.destroy', 'cn_name' => '删除角色', 'pid' => 6, 'guard_name' => 'api'],
            ['name' => 'roles.permissions', 'cn_name' => '权限列表', 'pid' => 6, 'guard_name' => 'api'],

            ['name' => 'sections', 'cn_name' => '部门管理', 'pid' => 0, 'guard_name' => 'api'],
            ['name' => 'sections.status', 'cn_name' => '部门状态', 'pid' => 13, 'guard_name' => 'api'],
            ['name' => 'sections.index', 'cn_name' => '部门列表', 'pid' => 13, 'guard_name' => 'api'],
            ['name' => 'sections.store', 'cn_name' => '添加部门 ', 'pid' => 13, 'guard_name' => 'api'],
            ['name' => 'sections.show', 'cn_name' => '部门详情 ', 'pid' => 13, 'guard_name' => 'api'],
            ['name' => 'sections.update', 'cn_name' => '修改部门 ', 'pid' => 13, 'guard_name' => 'api'],
            ['name' => 'sections.destroy', 'cn_name' => '删除部门 ', 'pid' => 13, 'guard_name' => 'api'],

            ['name' => 'settings', 'cn_name' => '网站配置 ', 'pid' => 0, 'guard_name' => 'api'],
            ['name' => 'settings.index', 'cn_name' => '配置列表 ', 'pid' => 20, 'guard_name' => 'api'],
            ['name' => 'settings.show', 'cn_name' => '配置详情 ', 'pid' => 20, 'guard_name' => 'api'],
            ['name' => 'settings.update', 'cn_name' => '修改配置 ', 'pid' => 20, 'guard_name' => 'api'],

            ['name' => 'testList', 'cn_name' => '试卷管理', 'pid' => 0, 'guard_name' => 'api'],
            ['name' => 'testList.add', 'cn_name' => '添加所需试卷数据', 'pid' => 24, 'guard_name' => 'api'],
            ['name' => 'testList.selectQuestions', 'cn_name' => '添加所需试题数据', 'pid' => 24, 'guard_name' => 'api'],
            ['name' => 'testList.copy', 'cn_name' => '复制试卷', 'pid' => 24, 'guard_name' => 'api'],
            ['name' => 'testList.preview', 'cn_name' => '试卷预览', 'pid' => 24, 'guard_name' => 'api'],
            ['name' => 'testList.index', 'cn_name' => '试卷列表', 'pid' => 24, 'guard_name' => 'api'],
            ['name' => 'testList.store', 'cn_name' => '添加试卷', 'pid' => 24, 'guard_name' => 'api'],
            ['name' => 'testList.show', 'cn_name' => '试卷详情', 'pid' => 24, 'guard_name' => 'api'],
            ['name' => 'testList.update', 'cn_name' => '试卷修改', 'pid' => 24, 'guard_name' => 'api'],
            ['name' => 'testList.destroy', 'cn_name' => '删除试卷', 'pid' => 24, 'guard_name' => 'api'],

            ['name' => 'categories', 'cn_name' => '分类管理', 'pid' => 0, 'guard_name' => 'api'],
            ['name' => 'categories.index', 'cn_name' => '分类列表', 'pid' => 34, 'guard_name' => 'api'],
            ['name' => 'categories.store', 'cn_name' => '添加分类', 'pid' => 34, 'guard_name' => 'api'],
            ['name' => 'categories.show', 'cn_name' => '分类详情', 'pid' => 34, 'guard_name' => 'api'],
            ['name' => 'categories.update', 'cn_name' => '分类修改', 'pid' => 34, 'guard_name' => 'api'],
            ['name' => 'categories.destroy', 'cn_name' => '禁用分类', 'pid' => 34, 'guard_name' => 'api'],

            ['name' => 'questions', 'cn_name' => '题库管理', 'pid' => 0, 'guard_name' => 'api'],
            ['name' => 'questions.status', 'cn_name' => '题库状态', 'pid' => 40, 'guard_name' => 'api'],
            ['name' => 'questions.index', 'cn_name' => '题库列表', 'pid' => 40, 'guard_name' => 'api'],
            ['name' => 'questions.store', 'cn_name' => '添加题库', 'pid' => 40, 'guard_name' => 'api'],
            ['name' => 'questions.show', 'cn_name' => '题库详情', 'pid' => 40, 'guard_name' => 'api'],
            ['name' => 'questions.update', 'cn_name' => '题库修改', 'pid' => 40, 'guard_name' => 'api'],
            ['name' => 'questions.destroy', 'cn_name' => '删除题库', 'pid' => 40, 'guard_name' => 'api'],

            ['name' => 'examinations', 'cn_name' => '考场管理', 'pid' => 0, 'guard_name' => 'api'],
            ['name' => 'examinations.sections', 'cn_name' => '考场人员', 'pid' => 47, 'guard_name' => 'api'],
            ['name' => 'examinations.index', 'cn_name' => '考场列表', 'pid' => 47, 'guard_name' => 'api'],
            ['name' => 'examinations.store', 'cn_name' => '添加考场', 'pid' => 47, 'guard_name' => 'api'],
            ['name' => 'examinations.show', 'cn_name' => '考场详情', 'pid' => 47, 'guard_name' => 'api'],
            ['name' => 'examinations.update', 'cn_name' => '修改考场', 'pid' => 47, 'guard_name' => 'api'],
            ['name' => 'examinations.destroy', 'cn_name' => '删除考场', 'pid' => 47, 'guard_name' => 'api'],

            ['name' => 'login.log', 'cn_name' => '登录日志', 'pid' => 0, 'guard_name' => 'api'],
            ['name' => 'operation.log', 'cn_name' => '操作日志', 'pid' => 0, 'guard_name' => 'api'],
        ];

        try {
            DB::beginTransaction();

            Permission::query()->insert($permissions);

            // 创建角色并分配已创建的权限
            Role::query()
                ->create([
                    'name' => 'super_admin',
                    'cn_name' => '超级管理员',
                    'guard_name' => 'api'
                ])
                ->givePermissionTo(Permission::all());

            DB::commit();
        } catch (Exception $exception) {
            DB::rollBack();
            dd($exception);
        }
    }
}
